package com.ts.integral.monitor;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.rabbitmq.client.Channel;
import com.ts.integral.dto.UserDto;
import com.ts.integral.service.UserService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

/**
 * @Author:zxc
 * @Description: 监听不同事件对用户的积分进行更新
 */
@Configuration
public class IntegralListnner {
    @Autowired
    UserService userService;

    //监听注册成功或者注销的用户id并且对该用户积分进行更新
    @RabbitListener(queues = "registersucess-Queue")
    public void registersucessListenner(UserDto userDto, Channel channel, Message message) {
        if (userDto.getOperation() == 1) {
            //增加积分100
            updateIntegral(userDto, 10,channel,message);
        } else {
            //积分为0
            UpdateWrapper updateWrapper = new UpdateWrapper();
            //设置需要更新的用户id
            updateWrapper.eq("id", userDto.getUserEntity().getId());
            //设置用户的新积分
            updateWrapper.set("integral", 0);
            //更新
            userService.update(updateWrapper);
        }
    }

    //监听有评论的用户并且对该用户积分进行更新
    @RabbitListener(queues = "disscuss-Queue")
    public void disscussQueueListenner(UserDto userDto, Channel channel, Message message) {
        if (userDto.getOperation() == 1) {
            //增加积分5
            updateIntegral(userDto, 5, channel, message);
        } else {
            //减少积分5
            updateIntegral(userDto, -5, channel, message);
        }
    }
    //监听发布帖子的用户并且对该用户积分进行更新
    @RabbitListener(queues = "publishPost-Queue")
    public void publishPostQueueListenner(UserDto userDto, Channel channel, Message message) {
        if (userDto.getOperation() == 1) {
            //增加积分10
            updateIntegral(userDto, 10, channel, message);
        } else {
            //减少积分10
            updateIntegral(userDto, -10, channel, message);
        }
    }
    //监听完善用户信息的用户并且对该用户积分进行更新
    @RabbitListener(queues = "finishMessage-Queue")
    public void finishMessageQueueListenner(UserDto userDto, Channel channel, Message message) {
        if (userDto.getOperation() == 1) {
            //增加积分50
            updateIntegral(userDto, 50, channel, message);
        } else {
            //减少积分50
            updateIntegral(userDto, -50, channel, message);
        }
    }
    //监听上传用户头像的用户并且对该用户积分进行更新
    @RabbitListener(queues = "uploadavatar-Queue")
    public void uploadavatarQueueListenner(UserDto userDto, Channel channel, Message message) {
        if (userDto.getOperation() == 1) {
            //增加积分50
            updateIntegral(userDto, 50, channel, message);
        } else {
            //减少积分50
            updateIntegral(userDto, -50, channel, message);
        }
    }
    //监听被点赞的用户并且对该用户积分进行更新
    @RabbitListener(queues = "like-Queue")
    public void likeQueueListenner(UserDto userDto, Channel channel, Message message) {
        if (userDto.getOperation() == 1) {
            //增加积分10
            updateIntegral(userDto, 10, channel, message);
        } else {
            //减少积分10
            updateIntegral(userDto, -10, channel, message);
        }
    }

    //更新用户积分
    private void updateIntegral(UserDto userDto, Integer degree, Channel channel, Message message) {
        //获得消息标识
        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        userDto.getUserEntity().setIntegral(userDto.getUserEntity().getIntegral() + degree);
        UpdateWrapper updateWrapper = new UpdateWrapper();
        //设置需要更新的用户id
        updateWrapper.eq("id", userDto.getUserEntity().getId());
        //设置用户的新积分
        updateWrapper.set("integral", userDto.getUserEntity().getIntegral());

        //应答成功
        try {
            //更新
            boolean update = userService.update(updateWrapper);
            if (update) {
                channel.basicAck(deliveryTag, false);
            }
        } catch (IOException e) {
            try {
                //存到redis中发布定时任务做mysql持久化

                //应答
                channel.basicNack(deliveryTag, false, false);
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }
}
